﻿using System.Collections.Generic;
using UnityEngine;

public class AnimeMgr<StateType> : IAnimeMgr<StateType> {
	readonly Animator _animator;
	readonly Dictionary<StateType, string> _animeDic;
	public AnimeMgr(Animator animator) {
		_animator = animator;
		_animeDic = new Dictionary<StateType, string>();
	}

	public void PauseAnim() => SetAnimPlaySpeed(0);

	public bool IsAnimPlayOver(int animLayerIndex)
		=> IsAnimPlayAt(1);

	public void ResumeAnim() => SetAnimPlaySpeed(1);

	public void SetAnimPlaySpeed(float speed)
		=> _animator.speed = Mathf.Clamp(speed, 0, 3);

	public void PlayAnim(StateType state) {

#if UNITY_EDITOR
		if (!_animeDic.ContainsKey(state)) {
			Debug.Log($"State {state} not in State-Anim Dic");
		}
#endif
		_animator.Play(_animeDic[state], 0, 0);
	}

	public void AddAnim(StateType state, string animName) {
#if UNITY_EDITOR
		if (_animeDic.ContainsKey(state)) {
			Debug.Log($"{state} already in dic");
		}
#endif
		_animeDic.Add(state, animName);
	}

	public bool IsAnimPlayAt(float normalizeTime)
		=> _animator.GetCurrentAnimatorStateInfo(0).normalizedTime > normalizeTime;
}